package com.yksj.healthtalk.ui.messageleave;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.adapter.DoctorMessageAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.entity.LeaveMessage;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.recharge.ChoiseDateActivity;
import com.yksj.healthtalk.utils.TimeUtil;

/**
 * 公告板 客户端
 * 
 * @author Administrator
 */
public class DoctorMessageBoardClient extends BaseFragmentActivity implements
		OnClickListener,OnClickSureBtnListener,OnRefreshListener<ListView> {
	private String id;//公告板主人的id
	private PullToRefreshListView mPullToRefreshListView;
	private ListView mListView;
	private DoctorMessageAdapter adapter;
	private int loadNum=0;
	private TextView mText;//日期text显示
	private String date="";
	List<LeaveMessage> list=new ArrayList<LeaveMessage>();
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_message_board_client);
		initView();
		id = getIntent().getStringExtra("id");
		initData(id);
	}

	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("医生公告");
		if(getIntent().hasExtra("title"))
		titleTextV.setText(getIntent().getStringExtra("title"));
//		titleRightBtn2.setOnClickListener(this);
//		titleRightBtn2.setText("我要留言");
//		titleRightBtn2.setVisibility(View.VISIBLE);
		
		mText = (TextView) findViewById(R.id.date_text);
		mText.setOnClickListener(this);
		findViewById(R.id.calender).setOnClickListener(this);
		findViewById(R.id.calender_button).setOnClickListener(this);
		mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.hall);
		mListView = mPullToRefreshListView.getRefreshableView();
		mListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.list_line_layout, null));
		mPullToRefreshListView.setOnRefreshListener(this);
		adapter = new DoctorMessageAdapter(list,this,0);
		mListView.setAdapter(adapter);
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		loadNum++;
		initData(id);
	}

	private void initData(String id) {
		HttpRestClient.doHttpLookLeaveMessage(id,loadNum+"",date,
				new ObjectHttpResponseHandler(this) {
			@Override
			public Object onParseResponse(String content) {
				return adapter.onparseJson(content,loadNum);
			}
			@Override
			public void onSuccess(Object response) {
				if(response==null) return;
				adapter.mList.addAll((List<LeaveMessage>)response);
				adapter.notifyDataSetChanged();
			}
			@Override
			public void onFinish() {
				super.onFinish();
				mPullToRefreshListView.onRefreshComplete();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)return;
		switch (requestCode) {
		case 1000:
			if(data.hasExtra("tishi"))
			SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),data.getExtras().getString("tishi"));
			adapter.mList.clear();
			loadNum=0;
			initData(id);
			break;
		case 0:
			if(data!=null){
				Bundle extras = data.getExtras();
				if(extras.containsKey("all")){
					date="";
					adapter.mList.clear();
					loadNum=0;
					mText.setText("全部");
				}else{
					if(extras.containsKey("nothing"))return;
					String string = extras.getString("date");
					String string1 = extras.getString("year");
					mText.setText(string1+"-"+string);
					date=string1+string;
					loadNum=0;
					adapter.mList.clear();
					}
			}else{
					date="";
					mText.setText(TimeUtil.getMoneyTime().substring(0,4)+"");
			}
			initData(id);
			break;
		}
	}
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2://进入留言界面
			requestMessage();
			break;
		case R.id.calender:
		case R.id.date_text:
		case R.id.calender_button:
			intent=new Intent(this,ChoiseDateActivity.class);
			if(mText.getText().toString().length()<5){
				intent.putExtra("date", TimeUtil.getMoneyTime());
			}else{
				intent.putExtra("date", mText.getText().toString());
			}
			startActivityForResult(intent,0);
			break;
		}
	}

	private void requestMessage() {
		//192.168.16.118:8085/sms_web/DoctorMessageBoardsServlet?OPTION=GOMESSAGE&CUSTOMERID=4200
		RequestParams params=new RequestParams();
		params.put("OPTION", "GOMESSAGE");
		params.put("CUSTOMERID", id);
		params.put("MESSAGECUSTOMERID", SmartFoxClient.getLoginUserId());
		HttpRestClient.doHttpRequestMessage(
				params, 
				new JsonsfHttpResponseHandler(this){
					@Override
					public void onSuccess(int statusCode,
							JSONObject response) {
						super.onSuccess(statusCode, response);
						/**
						 * 能留言：{"info":"成功","code":"0"} 不能留言：{"info":"此医生尚未开通患者留言，您可以选择购买医生服务，享受更细致全面的诊疗服务","code":"1"}
						 */
						int value=response.getIntValue("code");
						if(value==0){
							Intent intent = new Intent(DoctorMessageBoardClient.this,DoctorMessageBoardLeaveMessage.class);
							intent.putExtra("id", id);
							startActivityForResult(intent, 1000);
						}else{
							SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),response.getString("info"));
						}
					}
		});
		
	}

	@Override
	public void onClickSureHander() {
	}
}
